#include <conio.h>
#include "thread.hpp"

class Display : public Win32Thread
{
public:
	Display() : Win32Thread() {}
	~Display() {}

private:
	void run();
};

void Display::run()
{
	while (canRun())
	{
		Sleep(500);
		_putch('.');
	}

	_putch('*');
}

class Input : public Win32Thread
{
public:
	Input() : Win32Thread() {}
	~Input() {}

private:
	void run();
	Display m_display;
};

void Input::run()
{
	// Start the Display thread.
	if (m_display.create())
		m_display.start();
	else
		return;

	bool quit = false;

	while (!quit)
	{
		if (_kbhit())
		{
			switch (_getch())
			{
			default:
				break;

			// Suspend Display thread
			case 's':
			case 'S':
				m_display.suspend();
				break;

			// Resume Display thread
			case 'r':
			case 'R':
				m_display.resume();
				break;

			// Shutdown Input and Display threads.
			case 'q':
			case 'Q':
			case 27:
				m_display.shutdown();
				m_display.join();
				quit = true;
				break;
			}
		}
	}
}

int main()
{
	Input input;

	if (input.create())
	{
		input.start();
		input.join();
	}

	_cputs("\r\nmain thread has now joined with input thread\r\n");
	return 0;
}
